package business_opportunity

import (
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/backend_controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/business_opportunity/customer"
	"github.com/gin-gonic/gin"
)

type CustomerGroupController struct {
	backend_controller.BackendBaseController
	customerGroupService customer.CustomerGroupServiceIface
}

func NewCustomerGroupController(
	customerGroupService customer.CustomerGroupServiceIface,
) *CustomerGroupController {
	return &CustomerGroupController{customerGroupService: customerGroupService}
}

func (c *CustomerGroupController) RegisterRouter(g *gin.RouterGroup) {
	g.GET("/business-opportunity/customer-group/list", c.GetCustomerGroupList)
	g.POST("/business-opportunity/customer-group/save", c.SaveCustomerGroup)
	g.GET("/business-opportunity/customer-group/detail", c.GetCustomerGroupDetail)
	g.POST("/business-opportunity/customer-group/del", c.DeleteCustomerGroup)
}

func (c *CustomerGroupController) GetCustomerGroupList(ctx *gin.Context) {
	request := &business_opportunity.GetCustomerGroupListRequest{}
	if err := ctx.ShouldBindQuery(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	response, err := c.customerGroupService.GetCustomerGroupList(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *CustomerGroupController) SaveCustomerGroup(ctx *gin.Context) {
	request := &business_opportunity.SaveCustomerGroupRequest{}
	if err := ctx.ShouldBindJSON(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	request.CreatedBy = authorize.GetLoginZhanTuUserName(ctx)
	response, err := c.customerGroupService.SaveCustomerGroup(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *CustomerGroupController) GetCustomerGroupDetail(ctx *gin.Context) {
	request := &business_opportunity.GetCustomerGroupDetailRequest{}
	if err := ctx.ShouldBindQuery(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	response, err := c.customerGroupService.GetCustomerGroupDetail(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *CustomerGroupController) DeleteCustomerGroup(ctx *gin.Context) {
	request := &business_opportunity.DeleteCustomerGroupRequest{}
	if err := ctx.ShouldBindJSON(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	response, err := c.customerGroupService.DeleteCustomerGroup(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}
